package com.tudouji.project.organization.controller;

import com.tudouji.common.enums.SupplierTypeEnums;
import com.tudouji.common.exception.CustomException;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.organization.domain.Supplier;
import com.tudouji.project.organization.mapper.SupplierMapper;
import com.tudouji.project.organization.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供应商Controller
 *
 * @author wf
 * @date 2020-12-25
 */
@RestController
@RequestMapping("/organization/supplier")
public class SupplierController extends BaseController {
	@Autowired
	private ISupplierService supplierService;
	@Autowired
	private SupplierMapper supplierMapper;

	/**
	 * 查询供应商列表
	 */
	@PreAuthorize("@ss.hasPermi('organization:supplier:list')")
	@GetMapping("/list")
	public TableDataInfo list(Supplier supplier) {
		startPage();
		List<Supplier> list = supplierService.selectSupplierList(supplier);
		return getDataTable(list);
	}

	/**
	 * 导出供应商列表
	 */
	@PreAuthorize("@ss.hasPermi('organization:supplier:export')")
	@Log(title = "供应商", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(Supplier supplier) {
		List<Supplier> list = supplierService.selectSupplierList(supplier);
		ExcelUtil<Supplier> util = new ExcelUtil<Supplier>(Supplier.class);
		return util.exportExcel(list, "supplier");
	}

	/**
	 * 获取供应商详细信息
	 */
//	@PreAuthorize("@ss.hasPermi('organization:supplier:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(supplierService.selectSupplierById(id));
	}

	/**
	 * 新增供应商
	 */
	@PreAuthorize("@ss.hasPermi('organization:supplier:add')")
	@Log(title = "供应商", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Supplier supplier) {
		Supplier suppliered = supplierMapper.selectSupplierByName(supplier.getName());
		if (null != suppliered){
			return AjaxResult.error("供应商已存在！");
		}
		/*if (supplier.getType().equals(SupplierTypeEnums.GROWER.getCode())) {
			if (supplier.getUserid() != null) {

			}
			Supplier s = new Supplier();
			s.setUserid(supplier.getUserid());
			List<Supplier> supplierList = this.supplierService.selectSupplierList(s);
			if (supplierList.size() > 0) {
				return AjaxResult.error("关联用户已经存在，请重新选择关联用户");
			}
		} else*/ if (supplier.getType().equals(SupplierTypeEnums.SUPPLIER.getCode())) {
			Supplier s = new Supplier();
			s.setOrgguid(supplier.getOrgguid());
			List<Supplier> supplierList = this.supplierService.selectSupplierList(s);
			if (supplierList.size() > 0) {
				return AjaxResult.error("关联组织已经存在，请重新选择关联组织");
			}
		}
		// supplier.setStatus(GeneralStatusEnums.NORMAL.getCode());
		return toAjax(supplierService.insertSupplier(supplier));
	}

	/**
	 * 修改供应商
	 */
	@PreAuthorize("@ss.hasPermi('organization:supplier:edit')")
	@Log(title = "供应商", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Supplier supplier) {

		if (supplier.getType().equals(SupplierTypeEnums.GROWER.getCode())) {
			Supplier s = new Supplier();
			s.setId(supplier.getId());
			s.setUserid(supplier.getUserid());
			List<Supplier> supplierList = this.supplierService.selectSupplierList(s);
			if (supplierList.size() > 0) {
				return AjaxResult.error("关联用户已经存在，请重新选择关联用户");
			}
		} else if (supplier.getType().equals(SupplierTypeEnums.SUPPLIER.getCode())) {
			Supplier s = new Supplier();
			s.setId(supplier.getId());
			s.setOrgguid(supplier.getOrgguid());
			List<Supplier> supplierList = this.supplierService.selectSupplierList(s);
			if (supplierList.size() > 0) {
				return AjaxResult.error("关联组织已经存在，请重新选择关联组织");
			}
		}
		return toAjax(supplierService.updateSupplier(supplier));
	}

	/**
	 * 删除供应商
	 */
	@PreAuthorize("@ss.hasPermi('organization:supplier:remove')")
	@Log(title = "供应商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(supplierService.deleteSupplierByIds(ids));
	}

	/**
	 * 查询供应商列表
	 */
	@GetMapping("/findSupploerlist")
	public AjaxResult findSupploerlist(Supplier supplier) {
		List<Supplier> list = supplierService.selectSupplierList(supplier);
		return AjaxResult.success(list);
	}

	/**
	 * 获取正常状态的物流公司
	 * @return
	 */
	@GetMapping("/getWlComp")
	public AjaxResult getWlComp() {
		Supplier supplier=new Supplier();
		supplier.setType(4);
		supplier.setStatus(1);
		List<Supplier> list = supplierService.selectSupplierList(supplier);
		return AjaxResult.success(list);
	}
}
